package com.shiguiwu.springmybatis.algorithm.tree;

import lombok.Data;

/**
 * @description: 二叉树
 * @author: stone
 * @date: Created by 2021/5/30 14:40
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.algorithm.tree
 */
public class BinaryTree {

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node1 = new HeroNode(2, "吴用");
        HeroNode node2 = new HeroNode(3, "卢俊义");
        HeroNode node3 = new HeroNode(4, "林冲");
        HeroNode node4 = new HeroNode(5, "关胜");
//        HeroNode node4 = new HeroNode(1, "宋江");
        root.left = node1;
        root.right = node2;
        node2.right = node3;
        node2.left = node4;

        binaryTree.root = root;

        System.out.println("前序==========================");
        //前序 1 2 3  5 4
        binaryTree.preOder();

        System.out.println("中序==========================");

        //中序 2 1 5 3 4
        binaryTree.midOrder();

        System.out.println("后序==========================");
        //后序 2 5 4 3 1
        binaryTree.postOrder();

    }

    private HeroNode root;

    /**
     * 前序遍历
     */
    public void preOder() {
        if (this.root != null) {
            this.root.preOrder();
        }
        else {
            System.out.println("二叉树为空，无法遍历！！！");
        }
    }


    /**
     * 中序遍历
     */
    public void midOrder() {
        if (this.root != null) {
            this.root.midOrder();
        }
        else {
            System.out.println("二叉树为空，无法遍历！！！");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        }
        else {
            System.out.println("二叉树为空，无法遍历！！！");
        }
    }




    @Data
    public static class HeroNode {
        private int no;

        private String name;

        private HeroNode left;

        private HeroNode right;

        public HeroNode(int no, String name) {
            this.no = no;
            this.name = name;
        }

        //前序遍历
        public void preOrder() {
            //先输出父节点
            System.out.println(this);
            //左子树
            if (this.left != null) {
                this.left.preOrder();
            }

            //右子树
            if (this.right != null) {
                this.right.preOrder();
            }


        }

        /**
         * 中序遍历
         */
        public void midOrder() {
            //左子树
            if (this.left != null) {
                this.left.midOrder();

            }
            //输出当前节点
            System.out.println(this);

            //遍历右子树
            if (this.right != null) {
                this.right.midOrder();
            }
        }

        //后序遍历
        public void postOrder() {
            //左子树
            if (this.left != null) {
                this.left.postOrder();

            }

            //遍历右子树
            if (this.right != null) {
                this.right.postOrder();
            }
            //输出当前节点
            System.out.println(this);


        }

        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    '}';
        }


    }
}


